package com.cangxuan.pushservice.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.pushservice.utils.HttpUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 手机Notification消息推送
 * 彭伟辰 完成
 */
@RestController
public class NotificationController extends BaseController {
    Map<String,String> header=new HashMap<>();

    @Value("${jpush.url}")
    String url;

    @Value("${jpush.user}")
    String user;

    @Data
    public static class MessageData {
        private String registrationId;  //JPush设备ID
        private String message;         //消息
    }

    public Map<String, String> getHeader() {
        if(header.get("Authorization")==null){
            String userNameAndPassWord= Base64.getEncoder().encodeToString(user.getBytes(StandardCharsets.UTF_8));
            header.put("Authorization","Basic "+userNameAndPassWord);
        }
        return header;
    }

    /**
     * 向指定用户的手机推送消息【优先完成】
     * @param MessageData
     * @return
     */
    @PostMapping("pushMessageToUser")
    public Object pushMessageToUser(@RequestBody MessageData MessageData) {
        Map<String, String> header = getHeader();
        String registrationId = MessageData.getRegistrationId(); //JPush设备ID
        String message = MessageData.getMessage();              //推送内容
        JSONObject jsIntent=new JSONObject();
        JSONObject jj=new JSONObject();
        jj.put("intent",jsIntent);
        jsIntent.put("url","intent:#Intent;action=android.intent.action.MAIN;end");
        JSONObject js=new JSONObject();
        js.put("alert",message);
        js.put("android",jj);
        JSONObject jsRe=new JSONObject();
        jsRe.put("registration_id",new String[]{registrationId});
        //在这里，调用极光的点对点推送接口，对指定的用户手机推送消息
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("platform",new String[]{"android"});
        jsonObject.put("audience", jsRe);
        jsonObject.put("notification",js);
        System.out.println("js::"+js);
        String post = HttpUtils.post(url, header, jsonObject.toJSONString());
        JSONObject parse = JSONObject.parseObject(post);
        if(parse.getObject("error", Map.class)!=null){
            return success("访问失败", null);
        }
        return success("OK");
    }

    /**
     * 向所有用户手机推送消息
     * @param message
     * @return
     */
    @PostMapping("pushMessage")
    public Object pushMessage(@RequestBody String message) {
        //在这里，调用极光的批量推送，将该消息推到咱们所有APP用户手机
        Map<String, String> header1 = getHeader();
        JSONObject js=new JSONObject();
        JSONObject jsIntent=new JSONObject();
        JSONObject jj=new JSONObject();
        jj.put("intent",jsIntent);
        jsIntent.put("url","intent:#Intent;action=android.intent.action.MAIN;end");
        js.put("alert",message);
        js.put("android",jj);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("platform",new String[]{"android"});
        jsonObject.put("audience", "all");
        jsonObject.put("notification",js);
        String post = HttpUtils.post(url, header1, jsonObject.toJSONString());
        JSONObject parse = JSONObject.parseObject(post);
        if(parse.getObject("error", Map.class)!=null){
            return success("访问失败", null);
        }
        return success("OK");
    }
}
